package com.itheima;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class MyInetAddressDemo2 {
        public static void main(String[] args) {
            String hostname = "localhost"; // 目标主机名或IP地址
            int port = 12345; // 目标端口号
            String message = "Hello, UDP!"; // 要发送的消息

            try (DatagramSocket socket = new DatagramSocket()) {
                InetAddress address = InetAddress.getByName(hostname);
                byte[] buffer = message.getBytes();

                DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
                socket.send(packet);

                System.out.println("消息已发送: " + message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



}
